//
//  YXForumTableView.m
//  CreditCard
//
//  Created by 杨旭 on 2017/12/27.
//  Copyright © 2017年 杨旭. All rights reserved.
//

#import "YXForumTableView.h"
#import "YXForumCell.h"
#import "YXPostModel.h"
@interface YXForumTableView ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic ,strong) UIView *headerView;
@end

@implementation YXForumTableView

- (UIView *)headerView {
    if (!_headerView) {
        _headerView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, kScreenWidth, 200))];
        UIImageView *headerImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_1"]];
        headerImage.frame = _headerView.bounds;
        [_headerView addSubview:headerImage];
    }
    return _headerView;
}

- (void)setListArr:(NSMutableArray *)listArr {
    _listArr = listArr;
    for (YXPostModel *model in _listArr) {
//        [model setIsSelectButton:NO];
    }
    [self reloadData];
}


- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style {
    if (self = [super initWithFrame:frame style:style]) {
        self.backgroundColor = color_backColor;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.delegate =self;
        self.dataSource = self;
        self.tableHeaderView = self.headerView;
        [self registerNib:[UINib nibWithNibName:@"YXForumCell" bundle:nil] forCellReuseIdentifier:@"YXForumCell"];
    }
    return self;
}
#pragma mark - UItableView Delegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.listArr.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXForumCell *cell = [tableView dequeueReusableCellWithIdentifier:@"YXForumCell" forIndexPath:indexPath];
    cell.model = self.listArr[indexPath.section];
    cell.tag = 10000+indexPath.section;
    [cell.replyBtn addTarget:self action:@selector(replyBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.likeBtn addTarget:self action:@selector(likeBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    [cell.nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    YXPostModel *model = self.listArr[indexPath.section];
    return model.cellHeight;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [[UIView alloc] init];
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 15;
}

- (void)replyBtnAction:(UIButton *)sender {
    UIView *cell = [[sender superview] superview];
    NSInteger cellTag = cell.tag - 10000;
    YXPostModel *cellModel = _listArr[cellTag];
    self.clickReplyBtnBlock(cellModel.ID);
}


- (void)likeBtnAction:(UIButton *)sender {
    UIView *cell = [[sender superview] superview];
    NSInteger cellTag = cell.tag - 10000;
    YXPostModel *cellModel = _listArr[cellTag];
//    if (sender.selected) {
//        sender.selected = NO;
//
//        if ([cellModel.collectCount integerValue] > 1) {
//            NSInteger count = [cellModel.collectCount integerValue];
//            [cellModel setCollectCount:[NSString stringWithFormat:@"%ld",count -1]];
//        }
//        [sender setTitle:[NSString stringWithFormat:@"%ld",cellModel.collectCount.integerValue] forState:(UIControlStateNormal)];
//    }else {
//        sender.selected = YES;
//
//        NSInteger count = [cellModel.collectCount integerValue];
//        [cellModel setCollectCount:[NSString stringWithFormat:@"%ld",count +1]];
//        [sender setTitle:[NSString stringWithFormat:@"%@",cellModel.collectCount] forState:(UIControlStateNormal)];
//    }
    self.clickCollerBtnBlock(sender, cellModel);

}

- (void)nextBtnAction:(UIButton *)sender {
    UIView *cell = [[sender superview] superview];
    NSInteger cellTag = cell.tag - 10000;
    YXPostModel *cellModel = _listArr[cellTag];
    self.selectRowAtIndexPath(cellModel.ID);
}

@end
